<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="输入分类名称" />
      <el-button v-permission="['operation:category:list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <!--    <el-button v-permission="['operation:category:create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button v-permission="['operation:category:batchdelete']" :disabled="selectedIds.length === 0" class="filter-item" type="danger" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
   -->
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >

      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="ID" prop="id" />
      <el-table-column align="center" min-width="100" label="分类名称" prop="name" />

      <!--<el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['operation:category:edit']" type="primary" size="mini" @click="handleUpdate(scope.row,scope.$index)">编辑</el-button>
          <el-button v-permission="['operation:category:delete']" type="danger" style="margin:5px" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
    <el-dialog :visible.sync="attributeVisiable" title="设置新闻分类">
      <el-form
        ref="attributeForm"
        :model="attributeForm"
        :rules="attrRules"
        :data = "attributes"
        status-icon
        label-position="left"
        label-width="150px"
        style="width: 500px; margin-left:50px;"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="attributeForm.name" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="attributeVisiable = false">取消</el-button>
        <el-button type="primary" @click="handleAttributeAdd">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { categoryList, deleteCategory, batchDeleteCategory, createCategory, editCategory } from '@/api/categorylist'
import BackToTop from '@/components/BackToTop'
import { MessageBox } from 'element-ui'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ParamList',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        limit: 20,
        name: undefined
      },
      goodsDetail: '',
      detailDialogVisible: false,
      selectedIds: [],
      attributeVisiable: false,
      attributeForm: { name: '' },
      attrRules: {
        name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      attributes: [],
      editIndex: -1
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      categoryList(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate() {
      this.attributeForm = {}
      this.attributeVisiable = true
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除分类--' + row.name + '--, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCategory(row.id)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    },
    handleBatchDelete(e) {
      // 执行批量删除
      this.$confirm('此操作将永久删除选中的' + this.selectedIds.length + '个分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteCategory(this.selectedIds)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            this.getList()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    },
    handleSelectionChange(e) {
      const temp = []
      e.forEach(item => {
        temp.push(item.id)
      })
      this.selectedIds = temp
    },
    handleUpdate(row, index) {
      this.attributeForm = row
      this.attributeVisiable = true
      this.editIndex = index
    },
    handleAttributeAdd() {
      this.$refs['attributeForm'].validate(valid => {
        if (valid) {
          const finalCont = {
            ...this.attributeForm
          }
          if (finalCont.id) {
            editCategory(finalCont)
              .then(response => {
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
                this.list.splice(this.editIndex, 1, finalCont)
                this.attributeVisiable = false
              })
              .catch(response => {
                MessageBox.alert('业务错误：' + response.data.errmsg, '警告', {
                  confirmButtonText: '确定',
                  type: 'error'
                })
                this.attributeVisiable = false
              })
          } else {
            createCategory(finalCont)
              .then(response => {
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
                const temp = this.list
                temp.unshift(response.data.data)
                this.list = temp
                this.attributeVisiable = false
              })
              .catch(response => {
                MessageBox.alert('业务错误：' + response.data.errmsg, '警告', {
                  confirmButtonText: '确定',
                  type: 'error'
                })
                this.attributeVisiable = false
              })
          }
        }
      })
    }
  }
}
</script>
